package com.wl.shixun5.dao;

import com.wl.shixun5.util.Bookjdbc_Util;
import com.wl.shixun5.info.OrderInfo;
import com.wl.shixun5.info.Orderdetail;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 订单信息及订单详细DAO类
 */
public class OrderInfo_Orderdetail_DAO {
    
    /**
     * 生成订单编号
     * @return 订单编号（格式：yyyyMMddHHmmss）
     */
    public static String createOrderId() {
        String s = "";
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        s = df.format(new Date());
        return s;
    }

    /**
     * 插入订单信息
     * @param oi 订单信息对象
     * @return 影响的行数
     */
    public static int insertOrderInfo(OrderInfo oi) {
        String sql = "insert into orderinfo "
                + "(orderId,userId,receiverName,receiverAdd,receiverZip,orderMemo,orderPrice,orderDate,isSale)"
                + " values(?,?,?,?,?,?,?,?,?)";
        int i = 0;
        try {
            Connection cnn = Bookjdbc_Util.getConnection();
            PreparedStatement ps = cnn.prepareStatement(sql);
            ps.setString(1, oi.getOrderId());
            ps.setString(2, oi.getUserId());
            ps.setString(3, oi.getReceiverName());
            ps.setString(4, oi.getReceiverAdd());
            ps.setString(5, oi.getReceiverZip());
            ps.setString(6, oi.getOrderMemo());
            ps.setFloat(7, oi.getOrderPrice());
            ps.setDate(8, oi.getOrderDate());
            ps.setInt(9, oi.getIsSale());
            i = ps.executeUpdate();
            if (ps != null) {ps.close();}
            if (cnn != null) {cnn.close();}
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    /**
     * 插入订单详细信息
     * @param od 订单详细对象
     * @return 影响的行数
     */
    public static int insertOrderdetail(Orderdetail od) {
        String sql = "insert into orderdetail (orderId,bookId,bookCount) values(?,?,?)";
        int i = 0;
        try {
            Connection cnn = Bookjdbc_Util.getConnection();
            PreparedStatement ps = cnn.prepareStatement(sql);
            ps.setString(1, od.getOrderId());
            ps.setString(2, od.getBookId());
            ps.setInt(3, od.getBookCount());
            i = ps.executeUpdate();
            if (ps != null) {ps.close();}
            if (cnn != null) {cnn.close();}
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    /**
     * 查询用户的所有订单
     * @param userId 用户ID
     * @return 订单列表
     */
    public static List<OrderInfo> findOrderByUserId(String userId) {
        List<OrderInfo> li = new ArrayList<OrderInfo>();
        String sql = "select * from orderinfo where userId=? order by orderDate desc";
        try {
            Connection cnn = Bookjdbc_Util.getConnection();
            PreparedStatement ps = cnn.prepareStatement(sql);
            ps.setString(1, userId);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                OrderInfo oi = new OrderInfo();
                oi.setId(rs.getInt("Id"));
                oi.setOrderId(rs.getString("orderId"));
                oi.setUserId(rs.getString("userId"));
                oi.setReceiverName(rs.getString("receiverName"));
                oi.setReceiverAdd(rs.getString("receiverAdd"));
                oi.setReceiverZip(rs.getString("receiverZip"));
                oi.setOrderMemo(rs.getString("orderMemo"));
                oi.setOrderPrice(rs.getFloat("orderPrice"));
                oi.setOrderDate(rs.getDate("orderDate"));
                oi.setIsSale(rs.getInt("isSale"));
                li.add(oi);
            }
            if (rs != null) {rs.close();}
            if (ps != null) {ps.close();}
            if (cnn != null) {cnn.close();}
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return li;
    }

    /**
     * 根据订单ID查询订单详细
     * @param orderId 订单ID
     * @return 订单详细列表
     */
    public static List<Orderdetail> findOrderdetailByOrderId(String orderId) {
        List<Orderdetail> li = new ArrayList<Orderdetail>();
        String sql = "select * from orderdetail where orderId=?";
        try {
            Connection cnn = Bookjdbc_Util.getConnection();
            PreparedStatement ps = cnn.prepareStatement(sql);
            ps.setString(1, orderId);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Orderdetail od = new Orderdetail();
                od.setId(rs.getInt("Id"));
                od.setOrderId(rs.getString("orderId"));
                od.setBookId(rs.getString("bookId"));
                od.setBookCount(rs.getInt("bookCount"));
                li.add(od);
            }
            if (rs != null) {rs.close();}
            if (ps != null) {ps.close();}
            if (cnn != null) {cnn.close();}
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return li;
    }
}
